package com.yc.example.http;

import com.google.gson.JsonElement;
import com.yc.example.message.UserMessage;
import com.yc.mvplib.Retrofit.net.ApiService;
import com.yc.mvplib.Retrofit.net.NetCallBack;
import com.yc.mvplib.Retrofit.net.RetrofitFactory;
import com.yc.mvplib.Retrofit.net.RxSchedulers;
import com.yc.mvplib.utlis.DESUtil;
import com.yc.mvplib.utlis.GsonUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 描述：TODO
 * 创建者：  Churck
 * 创建时间：2017/12/8 15:22
 * 修改者： Churck
 * 修改时间：2017/12/8 15:22
 * 版 本：v1.0
 */

public class RequestBodyParam {
    private static final String File_key="A3712C5D2F98263F8C9219E4DA604E2F";
    private static volatile RequestBodyParam instance;
    private RequestBodyParam(){}
    public static RequestBodyParam getInstant() {
        if (instance == null) {
            synchronized (RequestBodyParam.class) {
                if (instance == null) {
                    instance = new RequestBodyParam();
                }
            }
        }
        return instance;
    }
    /**
     * 领奖
     */
    public  void updateLotteryStatus(String id,String qqnumber,NetCallBack<JsonElement> callback){
        Map<String, String> map = new HashMap<>();
        map.put("userid", UserMessage.getInstance().getUserid());
        map.put("id", id );
        map.put("qqnumber",qqnumber );
        String data = GsonUtil.getInstance().GsonString(map);
        String sid="";
        setNetParam(sid,data,callback);

    }

    private void setNetParam(String sid,String data,NetCallBack<JsonElement> callback){
        Map<String, String>  map= toDataMap(sid,data);

        RequestBody bodyParam= RequestBody.create(MediaType.parse("application/json;charset=utf-8"), GsonUtil.getInstance().GsonString(map));
        Observable observable= RetrofitFactory.getInstence().getRetrofit(ApiService.class).commonRequest(bodyParam);
        observable.compose(RxSchedulers.compose()).subscribe(callback);
    }

    /**
     * 合成数据的map对象,只传Userid
     * @param： sid参数/接口名
     * @param:
     * @return 合成的map对象
     */
    private Map<String, String> toDataMap(String sid){
        String useid=  UserMessage.getInstance().getUserid();
        Map<String, String> hm = new HashMap<>();
        hm.put("userid", useid);
        String data = GsonUtil.getInstance().GsonString(hm);
        return toDataMap(sid, data);
    }
    /**
     * 合成数据的map对象
     * @param： sid参数/接口名
     * @param:data参数/接口需要的参数转换的josn字符串
     * @return 合成的map对象
     */
    private Map<String, String> toDataMap(String sid, String data){

        Map<String, String> hm = new HashMap<>();
        if (sid != null) {
            hm.put("sid", DESencrypt(sid));
        }
        if (data != null) {
            hm.put("data", DESencrypt(data));
        }

        return hm;

    }
    /**
     * DES加密
     * @param要加密的字符串
     * @return 加密后的字符串
     * @throws Exception
     */
    private  String DESencrypt(String str){
        String encrypt = null;
        try {
            encrypt = DESUtil.encrypt(str, File_key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encrypt;
    }

}
